package Pages;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import clientMain.*;

/**
 * Upload Images Page
 * @author Andrew Fox
 *
 */

public class UploadImagesPage extends Page {
	HashMap<String, StringBuffer> data=new HashMap<String, StringBuffer>();
	ArrayList<File> pics=new ArrayList<File>();
	int numPicsToSend;
	
	public UploadImagesPage(Page page){
		super(page.url,page.html,page.client,page.pageType);
		this.pageOpenTime=page.pageOpenTime;
		this.lastURL=page.lastURL;
	}

	/**
	 * Determines the next link to go to in the application
	 * @return the URL or HTML of the next link, depending on whether
	 * 		   the previous entry contained a form or not
	 */
	public StringBuffer makeDecision() throws Exception{
		StringBuffer nextPage=new StringBuffer();

		int numPicsToSend=0;
		
		double numImagesProb=Math.random();
		for(Double m:clientGenerator.numHeaderImages.keySet()){
			if (numImagesProb<m){
				numPicsToSend=clientGenerator.numHeaderImages.get(m);
				break;
			}
		}
		
		if(verbose)System.out.println("Number of Item Images: "+numPicsToSend);
		data=getFormData();
		for (int i=0;i<numPicsToSend;i++){
			pics.add(new File(clientGenerator.itemPics.get((int)(Math.random()*clientGenerator.itemPics.size()))));
		}

		// Think Time
		try{Thread.sleep(getThinkTime());}
		catch(InterruptedException e){
			e.printStackTrace();
		}
		
		nextPage=doSubmitPic2(new StringBuffer(runSettings.rootURL).append("/sellitemimages?").toString(),data,pics);
	//nextPage=doSubmitPic(new StringBuffer(runSettings.appURL).append("/sellitemimages?"),data,pics);

		return nextPage;
	}
	
	/**
	 * Determines the think time before going to the next page
	 * Deducts from the think time the amount of time since the 
	 * page opened until this calculation is performed
	 * @return Think time in ms
	 */
	public int getThinkTime(){
		int thinkTime=(int)expDist(initialThinkTime);
		int firstPictureFindTime=(int)expDist(10000);
		int lastPictureFindTime=(int)expDist(1000);

		if (numPicsToSend==1)
		thinkTime+=firstPictureFindTime;
		else if(numPicsToSend>1)
			thinkTime+=firstPictureFindTime+lastPictureFindTime*(numPicsToSend-1);
		

		if (verbose)System.out.println("User: "+client.userInfo.username+" - Think Time: "+thinkTime+" ms");
		return (int) ((thinkTime-(new Date().getTime()-pageOpenTime))/runSettings.thinkTimeSpeedUpFactor);
	}
}